package com.atguigu.gulimail.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimail.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2016102100734469";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key ="MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCIOLMhEZqsJmpdE/WLgAjNIpyE5c1OvqHH3zzmlb9gDf6w25ItALceoEZ8nh3CiXFtJWK7gjYtcSyV+l4IbSOo8b9XeAqHk+/Nh5HpoX/BdcLgyShwXLm1TiCRxrlouaJH4crcl+GFQ55M1uSKRFAmEQ1H/bjSLg1/+JmoU08BO50d1khrxSxQ6YfPIv0OFhtDi5JhlA15pUy9KevI2aZV1cXeDGQB+TBfgcFp7m6z9ojCMblHMu+KVFmQ1bsKWqLteMgvRE9YeA7iz1ipHjhDFgIIJoMTjUku1veuXH8b0q7FX1ZMDNJmwXgCBqSTCTC61ibqoAylO6L8XLtQHB4XAgMBAAECggEAe1BQoCvuh86S1MWfr3PtA75EUG4zSAh8LZJh8B85SjwBBpsOHZ0bKCdRB7ifGh8Pxg1k7nd2kfZ0tSQbgadC88o8O3NYnNkH6VfDd+qR21C+ZHzVFbprBry98RkjeLErPcUhvkXjip7mq4qRArXKlFs1WalOaT31TJaAiwc3atX4y8bd/ulwGC7XNsN4StTMddZ8QR51sF8EDxvUd8fALA/1bfzCaSC5RnYFBnKijgk2tmlSHA3H14jH5q3Icl3E9XXVolqOoasrN45XuhPF1rA1uSBvklY+5HxQh4glPnxc8MB79b26jbj0XSuNHoSE7l+3I2U1hsZ1R0PonyT6mQKBgQDFYHbB528gpaIb/LspGYlheFIdF+PF7Ctlm9Y3aj1ov5LgBsoC1GfwWPo7eGLGM1eHqngMVKQ+hUjBMf8z+OR2H3zHmyt6evyJ8bWaHukWlai7XocrGTqdjATQZPwQ+w+JqPFnzP5Da3Usm5uAiwsYnNYxUk2kqsXXEyVDpKE4JQKBgQCwrkse2BXb4rk70qK5XDbpJz06qAsHz6qQ+rUlGjYTxlbYILfbCBuSeQLb0wWwNMw5EtuV34qWmxcnRCjH5+BavwQsseeXo0Bj8zH9S+DpEgYYv7KYL9+rKZr/ioxtdfKeFENzdtIcnhGdEiDcJFOsSdaQDWfKvvodD7TNqz16iwKBgHq/S/u45zZha5iGKwsbU4GnZ0Gqmr966V4udSFPU9Nx2HC0KCtsVxt06mHTs3HZeiTn0fKxLNeo4h8r7a8vqT0AxgQIrxhjpexPwoWPlZuzBRVJazxVW0uhSVkkhFI3IGmXBFJufO4GxP8x4polXog2+UoBCkJO5eykfnc7yft1AoGABcZMkj+Bx2QLpdbmlqRa7xfnOmmPoru42XA4R3Qp3E+ZOwsmtzP2orztyP2GrBGtsxfYsXdS8kUzZoclxhLgFFc17bgK17vwONQUCDFYsoVySZDKZ4r5BoEfncaLfHBkc6I56iYyC9lmvfl/tXJCgdkP+DlX8Rp41kZTJH26Tq0CgYB0ezAuUYyoZFH1X+DESEERBe1SDtF+vuVBprRVttPoPwAGMWl5rwvxBWBsDLnleBcp25PCRKk/sbZdUsuauUbsQKlP5oFWYi2puBpZoI3P/TXV6vtxIA2H76NgpeCFIPAxVloCXG33U7uFz/TTSpyxA+nMjEkt+t6HLfav2DW7jg==";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAlk7Wp3QXZ4ySCIj+KNwx4yGCGiZkhDA6qS+r58sqXJCaL9HaPs0d/0CZ/ZnGW0lFX9qBxSBzN4LN1ipxxqfuVjNSMkXm0GLlNuh4DtUH1iSYjQKf3PBVYAMlHkxAcyL4w+kWB3sN36rlipQg8RsuXnMXHMlC+WJgYQe6Ilo2Vus0dJQ/lLQ4WK7sRj07qwEXGD0l6uKeHe0ou6FbevbFvXLWN9/8Y05PHGJDWIlNKDvN/ifwzxUx7G3o/6YfvwarFQ+F8KpNI3MGgCRKOiR9ZSPsw+5R/mKKqfP3tRiZ2M/gtowPbzizspELubOgzg0QHt0kiv2mSOkaSvDENIiB6QIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url = "http://order.free.idcfengye.com/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\"1m\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面

        return result;

    }
}
